# _*_ coding: utf-8 _*_
from odoo import models, fields, api

READONLY_STATES = {
    'draft': [('readonly', False)],
}


class StockPutOnShelfLine(models.Model):
    _name = "stock.put.on.shelf.line"

    shelf_id = fields.Many2one("stock.put.on.shelf")
    product_id = fields.Many2one("product.product", string=u"产品", readonly=True)
    lot_id = fields.Many2one("stock.production.lot", string=u"批次", readonly=True)
    product_qty = fields.Float(u"数量", readonly=True)
    src_location_id = fields.Many2one("stock.location", string=u"来源库位", domain=[('usage', '=', "internal")],
                                      readonly=True)
    dest_location_id = fields.Many2one("stock.location", string=u"目的库位", domain=[('usage', '=', "internal")],
                                       readonly=True, states=READONLY_STATES)
    state = fields.Selection(related="shelf_id.state")

    def create_move(self):
        return self.env['stock.move'].create({
            'name': self.product_id.name,
            'origin': self.shelf_id.name,
            'product_id': self.product_id.id,
            'product_uom': self.product_id.uom_id.id,
            'product_uom_qty': self.product_qty,
            'location_id': self.src_location_id.id,
            'location_dest_id': self.dest_location_id.id,
            'restrict_lot_id': self.lot_id.id,
        })

    def split(self):
        wizard = self.env["stock_put_on_shelf.split"].create({
            "shelf_line_id": self.id,
            "qty_available": self.product_qty,
        })
        self.env["stock_put_on_shelf.split.line"].create({
            "wizard_id": wizard.id,
            "location_id": self.dest_location_id.id
        })
        return {
            'type': 'ir.actions.act_window',
            'res_model': wizard._name,
            'res_id': wizard.id,
            'view_type': 'form',
            'view_mode': 'form',
            'target': 'new'
        }
